package com.bohan.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;

import com.bohan.main.Main;
import com.xingkong.behavior.model.Converter;

public class BoHanSevlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public BoHanSevlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String func_type = request.getParameter("func_type");
		String alg_type = request.getParameter("alg_type");
		String dataset=request.getParameter("dataset");
		if (func_type.equals("showtopk")) {
			System.out.println("showtopk");
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print("li\tan\nji\tjifd\niji\tfdsa");
			out.flush();
			out.close();
		}
		if (func_type.equals("givepara")) {
			if (alg_type.equals("kat")) {

				String beta = request.getParameter("beta");
				String topk = request.getParameter("topkkat");
				System.out.println(topk);
				String baseFilePath = getServletContext().getRealPath("/")
						+ "duansky" + File.separator;
				Main.setJason2Read(baseFilePath + "data" + File.separator
						+ dataset);
				String res = "";
				try {
					try {
						res = Main.linkPredPart2(Double.parseDouble(beta),
								Integer.parseInt(topk));
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// System.out.println(request.getParameter("beta"));
				response.setContentType("text/html");
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				JSONArray oh = new JSONArray();
				out.print(res);

				out.flush();
				out.close();
			}

			if (alg_type.equals("hit")) {
				String step = request.getParameter("step");

				String topk = request.getParameter("topkhit");
				String baseFilePath = getServletContext().getRealPath("/")
						+ "duansky" + File.separator;
				Main.setJason2Read(baseFilePath + "data" + File.separator
						+ dataset);
				String res = "";
				try {
					try {
						res = Main.linkPredPart2_hit(Integer.parseInt(step),
								Integer.parseInt(topk));
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// System.out.println(request.getParameter("beta"));
				response.setContentType("text/html");
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				JSONArray oh = new JSONArray();
				out.print(res);

				out.flush();
				out.close();
			}

			if (alg_type.equals("sim")) {
				String gamma = request.getParameter("gamma");
				String iter = request.getParameter("iter");

				String topk = request.getParameter("topksim");
				System.out.println(topk);
				String baseFilePath = getServletContext().getRealPath("/")
						+ "duansky" + File.separator;
				Main.setJason2Read(baseFilePath + "data" + File.separator
						+ dataset);
				String res = "";
				try {
					try {
						res = Main.linkPredPart2_sim(Double.parseDouble(gamma),
								Integer.parseInt(iter), Integer.parseInt(topk));
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// System.out.println(request.getParameter("beta"));
				response.setContentType("text/html");
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				JSONArray oh = new JSONArray();
				out.print(res);

				out.flush();
				out.close();
			}

		}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
